<?php
/***************************************************************************** *
* Class Name: PlayerDef
* Author: Nicholas Malacarne
* Description: Keeps track of a player's Blood/Ink/Hand and states.
***************************************************************************** */

class PlayerDef {

    private $blood,
            $ink,
            $hand,
            $name,
            $bDead,
            $status,
            $bTurn,
            $dieRoll,
            $maxBlood;

/***************************************************************************** *
*********************************CONSTRUCTOR!******************************** *
* Author: Nicholas Malacarne
* Parameters: $maxHealth - The maximum amount of health per player.
*             $playerName - The name of the player.
* Description: Basic set-up for PlayerDef obj. creation.
***************************************************************************** */
    function __construct($maxHealth, $playerName)
    {
     /* Assigns the player's initial state.
      */$this->blood = $maxHealth;
        $this->maxBlood = $maxHealth;
        $this->name = $playerName;
        $this->ink = 0;
        $this->bDead = false;
        $this->bTurn = false;
		$this->hand = array();

     /* Array contains the status effects which effect the player.
      * Does the player have the current status?
      * How many turns left until the status is removed?
      * The name of the status.
      */$this->status = array("Warded" => array("bStatus" => false,
                                                "turnsLeft" => null,
                                                "name" => "WARDED"),
                               "Burning" => array("bStatus" => false,
                                                  "turnsLeft" => null,
                                                  "name" => "BURNING"),
                               "Blinded" => array("bStatus" => false,
                                                  "turnsLeft" => null,
                                                  "name" => "BLINDED"),
                               "Cursed" => array("bStatus" => false,
                                                 "turnsLeft" => null,
                                                 "name" => "CURSED"),
                               "Blessed" => array("bStatus" => false,
                                                  "turnsLeft" => null,
                                                  "name" => "BLESSED"),
                               "Poisoned" => array("bStatus" => false,
                                                   "turnsLeft" => null,
                                                   "name" => "POISONED"),
                               "Dazed" => array("bStatus" => false,
                                                "turnsLeft" => null,
                                                "name" => "DAZED"),
                               "Weakened" => array("bStatus" => false,
                                                   "turnsLeft" => null,
                                                   "name" => "WEAKENED"));
    }

/***************************************************************************** *
* Function Name: rollDie
* Author: Nicholas Malacarne
* Parameters: NONE
* Description: Rolls a die for the player.
***************************************************************************** */
    function rollDie()
    {
        $this->dieRoll = rand(1, 10);
    }

/***************************************************************************** *
* Function Name: getRoll
* Author: Nicholas Malacarne
* Parameters: NONE
* Description: Returns the value stored in dieRoll.
***************************************************************************** */
    function getRoll()
    {
        return $this->dieRoll;
    }

/***************************************************************************** *
* Function Name: drawCards
* Author: Nicholas Malacarne
* Parameters: $handSize - The maximum hand size.
* Description: Used to determine which player's turn it is.
***************************************************************************** */
    public function drawCards($handSize)
    {
     /* How many cards does the player need to draw?
      */$drawAmt = $handSize - count($this->hand);

        for ($i = 0; $i < $drawAmt; $i++)
        {
            $cardType = rand(1, 6);

            switch ($cardType)
            {
                case 1:
                    $cardName = "Fire";
                    break;
                case 2:
                    $cardName = "Water";
                    break;
                case 3:
                    $cardName = "Earth";
                    break;
                case 4:
                    $cardName = "Wind";
                    break;
                case 5:
                    $cardName = "Life";
                    break;
                case 6:
                    $cardName = "Death";
                    break;
            }

         /* Creates the drawn card.
          */$newCard = new GlyphDef($cardName);

         /* Adds the card to the player's hand.
          */array_push($this->hand, $newCard);
        }
    }

/*TODO: Need to rework this.*/
/***************************************************************************** *
* Function Name: updateStatus
* Author: Nicholas Malacarne
* Parameters: $card - The played card.
* Description: Updates the player's status according to what card was played.
***************************************************************************** */
//    public function updateStatus($card)
//    {
//        $HTML = new SuperHTMLDef();
//
//     /* Was an effect updated?
//      */$bUpdated = false;
//
//     /* Assign beneficial effects to the player who cast the Glyph.
//      */if ($this->isTurn())
//        {
//            if ($card->isWarding())
//            {
//                $this->status["Warded"]["bStatus"] = true;
//                $this->status["Warded"]["turnsLeft"] = $card->getEffectLen();
//                
//                $HTML->addText($this->getName()." is ".
//                               $this->status["Warded"]["name"]." (".
//                               $this->status["Warded"]["turnsLeft"].")");
//
//                $HTML->newLine();
//
//                $bUpdated = true;
//
//            }
//
//            if ($card->isBlessing())
//            {
//                $this->status["Blessed"]["bStatus"] = true;
//                $this->status["Blessed"]["turnsLeft"] = $card->getEffectLen();
//
//                $HTML->addText($this->getName()." is ".
//                               $this->status["Blessed"]["name"]." (".
//                               $this->status["Blessed"]["turnsLeft"].")");
//
//                $HTML->newLine();
//
//                if ($this->status["Cursed"]["bStatus"])
//                {
//                    $HTML->addText($this->getName()." is no longer ".
//                               $this->status["Cursed"]["name"]);
//
//                    $HTML->newLine();
//
//                    $this->status["Cursed"]["bStatus"] = false;
//                }
//
//                $bUpdated = true;
//            }
//
//            if (!$card->getLifeGain() == 0)
//            {
//                $HTML->addText($this->assignLifeGain($card->getLifeGain()));
//                $bUpdated = true;
//            }
//        }
//        else
//        {
//            if ($card->isBurning())
//            {
//                $this->status["Burning"]["bStatus"] = true;
//                $this->status["Burning"]["turnsLeft"] = $card->getEffectLen();
//
//                $HTML->addText($this->getName()." is ".
//                               $this->status["Burning"]["name"]." (".
//                               $this->status["Burning"]["turnsLeft"].")");
//
//                $HTML->newLine();
//
//                $bUpdated = true;
//            }
//
//            if ($card->isBlinding())
//            {
//                $this->status["Blinded"]["bStatus"] = true;
//                $this->status["Blinded"]["turnsLeft"] = $card->getEffectLen();
//
//                $HTML->addText($this->getName()." is ".
//                               $this->status["Blinded"]["name"]." (".
//                               $this->status["Blinded"]["turnsLeft"].")");
//
//                $HTML->newLine();
//
//                $bUpdated = true;
//            }
//
//            if ($card->isCursing())
//            {
//                $this->status["Cursed"]["bStatus"] = true;
//                $this->status["Cursed"]["turnsLeft"] = $card->getEffectLen();
//
//                $HTML->addText($this->getName()." is ".
//                               $this->status["Cursed"]["name"]." (".
//                               $this->status["Cursed"]["turnsLeft"].")");
//
//                $HTML->newLine();
//
//                if ($this->status["Blessed"]["bStatus"])
//                {
//                    $HTML->addText($this->getName()." is no longer ".
//                                   $this->status["Blessed"]["name"]);
//
//                    $HTML->newLine();
//                    $this->status["Blessed"]["bStatus"] = false;
//                }
//
//                $bUpdated = true;
//            }
//
//            if ($card->isPoisoning())
//            {
//                $this->status["Poisoned"]["bStatus"] = true;
//                $this->status["Poisoned"]["turnsLeft"] = $card->getEffectLen();
//
//                $HTML->addText($this->getName()." is ".
//                               $this->status["Poisoned"]["name"]." (".
//                               $this->status["Poisoned"]["turnsLeft"].")");
//
//                $HTML->newLine();
//
//                $bUpdated = true;
//
//            }
//            
//            if ($card->isWeakening())
//            {
//                $this->status["Weakened"]["bStatus"] = true;
//                $this->status["Weakened"]["turnsLeft"] = $card->getEffectLen();
//
//                $HTML->addText($this->getName()." is ".
//                               $this->status["Weakened"]["name"]." (".
//                               $this->status["Weakened"]["turnsLeft"].")");
//
//                $HTML->newLine();
//
//                $bUpdated = true;
//
//            }
//
//            if ($card->isDazing())
//            {
//                if ($this->status["Warded"]["bStatus"])
//                {
//                    $HTML->addText($this->getName()." resists ".
//                                   $this->status["Dazed"]["name"]."!");
//
//                    $HTML->newLine();
//
//                    $bUpdated = true;
//                }
//                else
//                {
//                    $this->status["Dazed"]["bStatus"] = true;
//                    $this->status["Dazed"]["turnsLeft"] = $card->getEffectLen();
//
//                    $HTML->addText($this->getName()." is ".
//                                   $this->status["Dazed"]["name"]." (".
//                                   $this->status["Dazed"]["turnsLeft"].")");
//
//                    $HTML->newLine();
//
//                    $bUpdated = true;
//                }
//                
//            }
//
//            if ($card->isDiscard())
//            {
//                for ($i = 0; $i < $card->getDiscardAmt(); $i++)
//                {
//                    $cardNum = rand(0, count($this->hand) - 1);
//                    
//                    foreach ($this->hand as $key => $curCard)
//                    {
//                        if ($key == $cardNum)
//                        {
//                            $HTML->addText($this->getName().
//                                           " loses Glyph of ".
//                                           $curCard->getName()."!");
//
//                            $HTML->newLine();
//                            $this->setHand($cardNum);
//                        } 
//                    }
//                }
//
//                $bUpdated = true;
//            }
//
//            if ($card->isUnWarding())
//            {
//                if ($this->status["Warded"]["bStatus"])
//                {
//                    $this->status["Warded"]["bStatus"] = false;
//                    $HTML->addText($this->getName()." is no longer ".
//                                   $this->status["Warded"]["name"]);
//
//                    $HTML->newLine();
//
//                    $bUpdated = true;
//                }
//            }
//
//            if (!$card->getDmg() == 0)
//            {
//                $HTML->addText($this->assignDamage($card->getDmg()));
//                $bUpdated = true;
//            }
//
//            if (!$card->getLifeLoss() == 0)
//            {
//                $HTML->addText($this->assignLifeLoss($card->getLifeLoss()));
//                $bUpdated = true;
//            }
//        }
//
//        if ($bUpdated)
//        {
//            return $HTML->getPage();
//        }
//        else
//        {
//            return null;
//        }
//            
//    }

/***************************************************************************** *
* Function Name: decayStatus
* Author: Nicholas Malacarne
* Parameters: NONE
* Description: Sets each status' turnsLeft to a new value. (-1 every turn.)
***************************************************************************** */
    public function decayStatus()
    {
        foreach ($this->status as $key => $curStatus)
        {
            $this->status[$key]["turnsLeft"] = 
            $this->status[$key]["turnsLeft"] - 1;

            if ($this->status[$key]["turnsLeft"] <= 0)
            {
                $this->status[$key]["turnsLeft"] = 0;
                $this->status[$key]["bStatus"] = false;
            }
        }
    }

/*TODO: Rework?*/
/***************************************************************************** *
* Function Name: assignEffects
* Author: Nicholas Malacarne
* Parameters: NONE
* Description: Assigns damage/life loss/life gain from each status effect
 * on the player
***************************************************************************** */
//    public function assignEffects()
//    {
//        $HTML = new SuperHTMLDef();
//
//     /* Was an effect assigned?
//      */$bAssigned = false;
//
//        foreach ($this->status as $curStatus)
//        {
//            if ($curStatus["bStatus"])
//            {
//                if ($curStatus["name"] == "BURNING")
//                {
//                    $HTML->addText($this->assignDamage(10));
//                    $bAssigned = true;
//                }
//                else if ($curStatus["name"] == "CURSED")
//                {
//                    $HTML->addText($this->assignLifeLoss(5));
//                    $bAssigned = true;
//                }
//                else if ($curStatus["name"] == "BLESSED")
//                {
//                    $HTML->addText($this->assignLifeGain(5));
//                    $bAssigned = true;
//                }
//            }  
//        }
//
//        if ($bAssigned)
//        {
//            return $HTML->getPage();
//        }
//        else
//        {
//            return null;
//        }
//    }

/***************************************************************************** *
* Function Name: assignLifeLoss
* Author: Nicholas Malacarne
* Parameters: $lifeLoss - The amount of life the player should lose.
* Description: Assigns life loss on the player.
***************************************************************************** */
    public function assignLifeLoss($lifeLoss)
    {
        $report = "";

        $report .= $this->setBlood(-$lifeLoss);
        $report .= $this->getName()." loses ".$lifeLoss." Blood!\n";

        return $report;
    }

/***************************************************************************** *
* Function Name: assignLifeGain
* Author: Nicholas Malacarne
* Parameters: $lifeGain - The amount of life the player should gain.
* Description: Assigns damage/life loss/life gain from each status effect
 * on the player
***************************************************************************** */
    public function assignLifeGain($lifeGain)
    {
        $report = "";

     /* If the player is POISONED, we need to modify the amount they are
      * healed by.
      */if ($this->status["Poisoned"]["bStatus"])
        {
            $lifeMod = intval($lifeGain * .5);
            $report .= $this->setBlood($lifeMod);
            $report .= $this->getName()." gains ".$lifeMod." Blood!\n";
        }
        else
        {
            $report .= $this->setBlood($lifeGain);
            $report .= $this->getName()." gains ".$lifeGain." Blood!\n";
        }
        
        return $report;
    }

/***************************************************************************** *
* Function Name: assignDamage
* Author: Nicholas Malacarne
* Parameters: $mod - The amount of damage to assign to the player.
* Description: Assigns damage to the player.
***************************************************************************** */
    public function assignDamage($damage)
    {
        $report = "";

     /* Modify the damage if the player is currently WARDED.
      */if ($this->status["Warded"]["bStatus"])
        {
            $lifeMod = intval($damage * .5);
            $report .= $this->getName()." takes ".$lifeMod." damage!\n";
            $this->setBlood(-$lifeMod);
        }
        else
        {
            $report .= $this->getName()." takes ".$damage." damage!\n";
            $this->setBlood(-$damage);
        }

        return $report;
    }

/***************************************************************************** *
* Function Name: setBlood
* Author: Nicholas Malacarne
* Parameters: $mod - The amount to modify the player's Blood by.
* Description: Modifies the player's Blood.
***************************************************************************** */
    public function setBlood($mod)
    {
        $this->blood = $this->blood + $mod;

        if ($this->blood > $this->maxBlood)
        {
            $this->blood = $this->maxBlood;
        }

        if ($this->blood <= 0)
        {
            $this->blood = 0;
            $this->bDead = true;
        }
    }

    public function getBlood()
    {
        return $this->blood;
    }

    public function setInk($mod)
    {
        $this->ink = $this->ink + $mod;

        if ($this->ink < 0)
        {
            $this->ink = 0;
        }
    }
    
    public function getInk()
    {
        return $this->ink;
    }

    public function getName()
    {
        return $this->name;
    }

    public function getHand()
    {
        return $this->hand;
    }

    public function isDead()
    {
        return $this->bDead;
    }

    public function isTurn()
    {
        return $this->bTurn;
    }

    public function setTurn($turnMod)
    {
        $this->bTurn = $turnMod;
    }

    public function getStatusReport()
    {
     /* Formats each status the player currently has.
      */foreach ($this->status as $curStatus)
        {
            if ($curStatus["bStatus"])
            {
                $data .= $curStatus["name"]." (".$curStatus["turnsLeft"].")";
                $data .= "\n<br>\n";
            }
        }
        
        return $data;
    }
    
    public function removeCard($index)
    {
        array_splice($this->hand, $index, 1);
    }

    public function addHand($card)
    {
        array_push($this->hand, $card);
    }
    
    public function isDazed()
    {
        return $this->status["Dazed"]["bStatus"];
    }
    
}
?>
